import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        {
          path: '',
          component: () => import('@/views/Home/index.vue')
        },
        {
          path: 'Category/:id',
          component: () => import('@/views/Category//index.vue')
        },
        {
          path: 'Category/sub/:id',
          component: () => import('@/views/subCategory/index.vue')
        },
        {
          path: 'Detail/:id',
          component: () => import('@/views/Detail/index.vue')
        },
        {
          path: 'shopCart',
          component: () => import('@/views/shopCart/index.vue')
        },
        {
          path: 'checkOut',
          component: () => import('@/views/checkOut/index.vue')
        },
        {
          path: 'pay',
          component: () => import('@/views/Pay/index.vue')
        },
        {
          path: 'payback',
          component: () => import('@/views/Pay/PayBack.vue')
        },
        {
          path: 'member',
          component: () => import('@/views/MemberCenter/index.vue'),
          children: [
            {
              path: '',
              redirect: '/member/user'
            },
            {
              path: 'user',
              component: () => import('@/views/MemberCenter/components/userInfo.vue')
            },
            {
              path: 'order',
              component: () => import('@/views/MemberCenter/components/userOrder.vue')
            }
          ]
        }
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue')
    }
  ],
  // 路由滚动行为定制,切换页面回滚到顶部
  scrollBehavior() {
    return { top: 0, behavior: 'smooth' }
  }
})
// 全局前置守卫
router.beforeEach((to) => {
  if (to.path === '/login') {
    localStorage.clear()
  }
  return true
})

export default router
